class AdminController < ApplicationController
  layout 'main'
  include KinaMap
    def list
#        @filmy = Film.find_all
#        @filmy = FilmShort.find_all_from_ksk_page
        @filmy = FilmShort.get_ksk_movies
    end
    
   
    def rep
        render :partial => 'rep', :layout => 'main'
    end

    def read_kijow
#        repertuar = params[:repertuar]
        @kina = 'Apollo Kij�w i Studio'
        @data = (Date.today).to_s # jutro
        @read_data = Repertuar.read_kijow @data
#        raise @read_data.inspect
        render "admin/rep_data"
    end
    
    def insert_rep
      gen_start = Time.now
      @use_cache = CachedModel.use_memcache?  
      @dates = params["date"]
      @cine = params["page"]
      @reps = []
      case @cine
        when "KIJ"
          @kina = 'Apollo Kij�w i Studio i Mikro'
          @reps.concat(Repertuar.add_rep_for_kijow(@dates))
          @reps.concat(Repertuar.add_rep_for_mikro(@dates))
          @summary = Repertuar.check_reps_in_db @reps, @dates, [get_kina_map[:kijow], get_kina_map[:kijow_studio], get_kina_map[:mikro]]
          @gen_time = Time.now - gen_start
          render "admin/rep_summary"
        when "ARS"
          @kina = 'ARS'
          @reps.concat(Repertuar.add_rep_for_ars(@dates))
          @summary = Repertuar.check_reps_in_db @reps, @dates, [get_kina_map[:ars_aneks], get_kina_map[:ars_kiniarnia], get_kina_map[:ars_reduta], get_kina_map[:ars_salon], get_kina_map[:ars_sztuka]]
          @gen_time = Time.now - gen_start
          render "admin/rep_summary"
        when "MK"
          @kina = 'Multikino'
          @reps.concat(Repertuar.add_rep_for_mkino(@dates))
          @summary = Repertuar.check_reps_in_db @reps, @dates, [get_kina_map[:mkino]]
          @gen_time = Time.now - gen_start          
          render "admin/rep_summary"
        when "CC"
          @kina = 'Cinema City'
          @reps.concat(Repertuar.add_rep_for_cc(@dates))
          @summary = Repertuar.check_reps_in_db @reps, @dates, [get_kina_map[:cc_plaza], get_kina_map[:cc_zakopianka], get_kina_map[:cc_kazimierz], get_kina_map[:imax]]          
          @gen_time = Time.now - gen_start          
          render "admin/rep_summary"          
        when "SFI"
          @kina = 'SFI'
          @reps.concat(Repertuar.add_rep_for_sfinks(@dates))
          @summary = Repertuar.check_reps_in_db @reps, @dates, [get_kina_map[:sfinks]]
          @gen_time = Time.now - gen_start
          render "admin/rep_summary"     
        when "BAR"
          @kina = 'BAR'
          @reps.concat(Repertuar.add_rep_for_barany(@dates))
          @summary = Repertuar.check_reps_in_db @reps, @dates, [get_kina_map[:barany]]
          @gen_time = Time.now - gen_start
          render "admin/rep_summary"     
        else
          redirect_to :back 
      end
    end
    
    def add_foreign_film
      unless params["film"].blank?
        @film = URI.decode(params["film"])
        @kino = Kino.find(params["kino"])
        @obce_id =  URI.decode(params["obce_id"])
      else
        @film = ''
      end
    end
    
    def remove_rep
      rep = Repertuar.find(params[:id])
      if rep.destroy
        render_text "Usunieto"
      else
        render_text = "Usuniecie nie powiodlo sie"
      end
    end    
    
    def save_foreign_film
        form = params["form"]
        film = Film.find(form["film_id"])
        fo = FilmObcy.new
        fo.ID_film = film.ID_film
        fo.ID_kino = form["kino_id"]
        fo.obce_id = form["obce_id"]        
        fo.tytul = film.tytul
        fo.tytul_oryg = film.tytul_oryg
        begin
          fo.save
        rescue
          logger.debug("### Nie zpisano do bazy obiektu: #{fo.inspect} ####")
        end
        render "admin/add_foreign_film"
    end
    
end
